using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using common = Microsoft.ConnectionEngine.Common;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using business = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Business;

using Microsoft.ConnectionEngine.Common.Security;


namespace Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Facade
{
    class ServiceProviderTypeMessageTypeManager
    {
        //ServiceProviderTypeMessageTypeSearchRequest 1059
        public static void ServiceProviderTypeMessageTypeSearch(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            ServiceProviderTypeMessageTypes sptMessageTypes = new ServiceProviderTypeMessageTypes();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            String messageTypeID = xmlMessage.SelectSingleNode("//pnp:MessageTypeID", nsmgr).InnerText;
            String serviceProviderTypeID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderTypeID", nsmgr).InnerText;

            sptMessageTypes = business.MessageTypeManager.ServiceProviderTypeMessageTypeSearch(messageTypeID, serviceProviderTypeID);

            resultMsgType = (int)MessageTypes.ServiceProviderTypeMessageTypeSearchResult;
            resultXmlMessage = new XmlDocument();
            resultXmlMessage.LoadXml(common.Utilities.SerializeObject(sptMessageTypes, typeof(ServiceProviderTypeMessageTypes)));
        }

        //ServiceProviderTypeMessageTypesInsert 1058
        public static void ServiceProviderTypeMessageTypesInsert(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            ServiceProviderTypeMessageTypeLinks sptMessageTypeLinks = new ServiceProviderTypeMessageTypeLinks();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            XmlNode sptMessageTypeLinksNode = xmlMessage.SelectSingleNode("//pnp:ServiceProviderTypeMessageTypeLinks", nsmgr);
            sptMessageTypeLinks = (ServiceProviderTypeMessageTypeLinks)common.Utilities.DeserializeObject(sptMessageTypeLinksNode.OuterXml, typeof(ServiceProviderTypeMessageTypeLinks));

            //String status = business.MessageTypeManager.ServiceProviderTypeMessageTypeLinkInsert(sptMessageTypeLinks);
            business.MessageTypeManager.ServiceProviderTypeMessageTypeLinkInsert(sptMessageTypeLinks);

            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }

        //MessageTypeServiceProviderTypesAdd 1054
        public static void ServiceProviderTypeMessageTypeInsert(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            ServiceProviderTypeMessageTypeLink sptMessageTypeLink = new ServiceProviderTypeMessageTypeLink();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            XmlNode sptMessageTypeLinkNode = xmlMessage.SelectSingleNode("//pnp:ServiceProviderTypeMessageTypeLink", nsmgr);
            sptMessageTypeLink = (ServiceProviderTypeMessageTypeLink)common.Utilities.DeserializeObject(sptMessageTypeLinkNode.OuterXml, typeof(ServiceProviderTypeMessageTypeLink));

            //String status = business.MessageTypeManager.ServiceProviderTypeMessageTypeLinkInsert(sptMessageTypeLink);
            business.MessageTypeManager.ServiceProviderTypeMessageTypeLinkInsert(sptMessageTypeLink);

            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }

        //MessageTypeServiceProviderTypesDelete 1055
        public static void ServiceProviderTypeMessageTypeDelete(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            String serviceProviderTypeMessageTypeID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderTypeMessageTypeID", nsmgr).InnerText;
            //String status = business.MessageTypeManager.ServiceProviderTypeMessageTypeDelete(serviceProviderTypeMessageTypeID);
            business.MessageTypeManager.ServiceProviderTypeMessageTypeDelete(serviceProviderTypeMessageTypeID);

            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }

        //MessageTypeServiceProviderTypesListDelete 1069
        public static void ServiceProviderTypeMessageTypesDelete(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
        {
            ServiceProviderTypeMessageTypeLinks sptMessageTypeLinks = new ServiceProviderTypeMessageTypeLinks();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
            nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

            XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
            //HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
            Utilities.CreateUser(userCredentials, nsmgr);

            XmlNode sptMessageTypeLinksNode = xmlMessage.SelectSingleNode("//pnp:ServiceProviderTypeMessageTypeLinks", nsmgr);
            sptMessageTypeLinks = (ServiceProviderTypeMessageTypeLinks)common.Utilities.DeserializeObject(sptMessageTypeLinksNode.OuterXml, typeof(ServiceProviderTypeMessageTypeLinks));

            //String status = business.MessageTypeManager.ServiceProviderTypeMessageTypeLinkDelete(sptMessageTypeLinks);
            business.MessageTypeManager.ServiceProviderTypeMessageTypeLinkDelete(sptMessageTypeLinks);

            resultMsgType = 0;
            resultXmlMessage = new XmlDocument();
        }

    }
}
